package com.xja.model.vo.resp;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * 返回的消息体。result的内容是返回中的data中的内容
 * 前端接收到的返回体如下
 * {
 *     status:xxx
 *     data:{
 *         status:xxx
 *         msg:xxx
 *         data:{xxx}
 *     }
 * }
 * @author 蔡建国
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Result<T> {

    private Integer status;
    private String msg;
    private T data;

    public static Result<Void> success() {
        return new Result<>(200, "success", null);
    }

    public static<T> Result<T> success(T data) {
        return new Result<>(200, "success", data);
    }

    public static Result<Void> error(int code, String msg) {
        return new Result<>(code, msg, null);
    }

    public static Result<Void> error(String msg) {
        return error(500, msg);
    }

    // 自定义返回
    public static<T> Result<T> build(Integer code, String msg, T data) {
        Result<T> result = new Result<>();
        result.setStatus(code);
        result.setMsg(msg);
        result.setData(data);
        return result;
    }
}